﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SVC.client;

namespace UI.Forms
{
    public partial class ProductSaleBack : FormBase1
    {
        private SVC.client.ProductSaleBack_M m = null; //主表对象
        private BindingList<ProductSaleBack_D> ds = null; //明细表
        private string updjn = ""; //修改的单据号，区别该单据是新增还是修改

        public ProductSaleBack(string djpre)
        {
            InitializeComponent();
            updjn = djpre;
        }

        private void btn_select_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ds.Clear();
            //通过仓库ID，查询产品
            Select_Product frm = new Select_Product("");
            frm.FatherRefresh = import;
            frm.ShowDialog();
        }

        //绑定选中的产品信息
        void import(List<Product_Select> cs)
        {
            ds.Clear();
            foreach (Product_Select c in cs)
            {
                ProductSaleBack_D d = new ProductSaleBack_D();
                d.goodscode = c.GoodsCode;
                d.goodsname = c.GoodsName;
                d.unit = c.Unit;
                d.count = 1; //默认数量为1
                d.saleprice = c.outprice.Value; //参考售价
                d.backprice = c.outprice.Value; //退货单价
                d.saleamount = c.outprice.Value;
                d.backamount = c.outprice.Value; 
                ds.Add(d);
            }


        }

        //保存
        private void btn_OK_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dxValidationProvider1.Validate())
            {
                if (ds.Count==0)
                {
                    Comm.ShowAlert("请填写单据明细"); return;
                }
                m.djn = te_pid.Text.Trim(); //单据号
                m.remarks = te_remarks.Text.Trim(); //备注
                m.odate = dateEdit1.DateTime; //日期
                m.linkman = te_linkman.Text.Trim(); //联系人
                m.linktel = te_tel.Text.Trim(); //联系电话
                m.linkaddress = tel_address.Text.Trim(); //联系地址
                m.srpznum = txt_srpz.Text.Trim(); //收入凭证

                if (lue_JSempid.EditValue != null)
                {
                    m.jsuser = new Guid(lue_JSempid.EditValue.ToString()); //经手人
                }
                m.createuser = new Guid(lue_opID.EditValue.ToString()); //制单人
                m.warehouseID = lue_ck.EditValue.ToString(); //仓库
                m.clientID = new Guid(lue_Client.EditValue.ToString()); //客户

                m.ischeck = false;
                m.isfinish = false; //是否完结标志
                m.yfmoney = 0; //默认已付金额为0

                decimal total = 0, balance = 0;

                foreach (ProductSaleBack_D item in ds)
                {
                    total += item.saleamount.Value; //销售总价
                    balance += item.backamount; //退货总价
                }
                m.ProductSaleBack_D = ds;
                m.total = total;
                m.banlance = balance;
                m.discount = m.total - m.banlance; //盈亏金额

                string r = "";
                if (updjn == "")
                {
                    r = my.bw.ProductSaleBack_AddSave(m, my.ACID);
                    if (string.IsNullOrEmpty(r))
                    {
                        Comm.ShowInfo("新增销售退货单成功！");
                        this.Close();
                    }
                    else
                    {
                        Comm.ShowErorr(r);
                    }
                }
                else
                {
                    r = my.bw.ProductSaleBack_UpdateSave(m, my.ACID);
                    if (string.IsNullOrEmpty(r))
                    {
                        Comm.ShowInfo("修改销售退货单成功！");
                        this.Close();
                    }
                    else
                    {
                        Comm.ShowErorr(r);
                    }
                }
            }
        }


        //窗体加载事件
        private void ProductSale_Load(object sender, EventArgs e)
        {
            bind(); 
            if (updjn == "") //新增
            {
                this.Text = "销售退货单-新增";
            }
            else
            {
                this.Text = "销售退货单-修改";
                SetProductInfo();
            }
           
        }
        /// <summary>
        /// 窗体加载时调用方法
        /// </summary>
        private void bind()
        {
            m = new SVC.client.ProductSaleBack_M();
            ds = new BindingList<ProductSaleBack_D>(); //实例化销售单明细
            lue_ck.Properties.DataSource = my.bw.Warehouse_Get("", my.ACID); //绑定仓库列表
            lue_Client.Properties.DataSource = my.bw.View_Client_lite_List(false, my.ACID); //客户列表
            lue_JSempid.Properties.DataSource = my.bw.View_Emp_lite_List("", my.ACID); //业务员列表
            lue_opID.Properties.DataSource = my.bw.View_Emp_lite_List("", my.ACID);
            lue_opID.EditValue = my.emp_lite.EmplID;
            lue_JSempid.EditValue = my.emp_lite.EmplID;
            gridControl1.DataSource = ds; 
            dateEdit1.EditValue = DateTime.Now;
        }

        //日期控件的值改变事件
        //自动生成单据号
        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (dateEdit1.EditValue == null) return;
            te_pid.Text = my.bw.getBillNO(my.emp_lite.shopcode, dateEdit1.DateTime, 14, my.ACID);
            m.djn = te_pid.Text;
            foreach (SVC.client.ProductSaleBack_D d in ds)
            {
                d.djn = m.djn;
            }
        }

        private void lue_ck_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void lue_Client_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void btn_orderImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        #region ** 单价数量值改变后自动计算总金额 **
        
        /// <summary>
        /// 计算总金额方法
        /// </summary>
        private void SaleAmount(string field)
        {
            try
            {

                ProductSaleBack_D d = gridView1.GetRow(gridView1.FocusedRowHandle) as ProductSaleBack_D;
                string newvalue = "";
                if (gridView1.EditingValue != null)
                {
                    newvalue = gridView1.EditingValue.ToString();
                }
                else
                {
                    newvalue = gridView1.GetFocusedRowCellValue(field).ToString();
                }
                //判断是输入的哪个值
                switch (field)
                {
                    case "saleprice":
                        d.saleprice = decimal.Parse(newvalue);
                        break;
                    case "backprice":
                        d.backprice = decimal.Parse(newvalue);
                        break;
                    case "count":
                        d.count = decimal.Parse(newvalue);
                        break;
                    default:
                        break;
                }
                d.backamount = d.backprice * d.count;
                d.saleamount = d.saleprice * d.count;
            }
            catch
            {

            }

        }


        private void rice_jsprice_Leave(object sender, EventArgs e)
        {
            SaleAmount("backprice");
        }

        private void rise_quantity_Leave(object sender, EventArgs e)
        {
            SaleAmount("count");
        }

        #endregion


        #region 修改给控件赋值

        private void SetProductInfo()
        {
            ProductSaleBack_M mitem = my.bw.ProductSaleBack_MBydjn_Get(updjn, my.ACID);
            lue_Client.EditValue = mitem.clientID;
            lue_ck.EditValue = mitem.warehouseID;
            dateEdit1.EditValue = mitem.odate;
            te_pid.EditValue = updjn;
            te_linkman.EditValue = mitem.linkman;
            te_tel.EditValue = mitem.linktel;
            tel_address.EditValue = mitem.linkaddress;
            txt_srpz.EditValue = mitem.srpznum;
            te_remarks.EditValue = mitem.remarks;
            ds = my.bw.ProductSaleBack_DList_ByMdjn(updjn, my.ACID);
            gridControl1.DataSource = ds;
        }

        #endregion


        //从销售出库单中转入
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ProductSale_M_List frm = new ProductSale_M_List("back");
            frm.FatherRefresh = backimport;
            frm.ShowDialog();
        }

        //绑定选中的产品信息
        void backimport(ProductSale_M cs)
        {
            ds.Clear();

            if (cs != null)
            {
                //给单据主信息赋值
                lue_Client.EditValue = cs.clientID;
                lue_ck.EditValue = cs.warehouseID;
                //dateEdit1.EditValue = cs.odate;
                te_linkman.EditValue = cs.linkman;
                te_tel.EditValue = cs.linktel;
                tel_address.EditValue = cs.linkaddress;
                txt_srpz.EditValue = cs.srpznum;
                te_remarks.EditValue = cs.remarks;
                lue_JSempid.EditValue = cs.jsuser;

                //单据明细赋值
                foreach (ProductSale_D c in cs.ProductSale_D)
                {
                    ProductSaleBack_D d = new ProductSaleBack_D();
                    d.goodscode = c.goodscode;
                    d.goodsname = c.goodsname;
                    d.unit = c.unit;
                    d.count = c.count; //出库数量
                    d.saleprice = c.jsprice; //销售售价
                    d.backprice = c.jsprice; //退货单价
                    d.saleamount = c.jsamount;
                    d.backamount = c.jsamount;
                    ds.Add(d);
                }
            }

        }

    }
}